const { imgUrl } = getApp().globalData;
import { publicApi } from "@/utils/request/api/public";
import { mineApi } from "../../../../utils/request/api/mine";
import Toast from "@vant/weapp/toast/toast";

Page({
  data: {
    imgUrl: imgUrl + "tender/",
    keys: [],
    mainKey: "",
    hangye_type: "",
    area: "",
    title: "",
    gonggao_type: "招标公告",
    type: "",
    id: 0,
    industryList: [],
    citys: [],
    city_columns: [],
    showSelectIndustry: false,
    showSelectCity: false,
  },
  onLoad(options) {
    console.log("options === ", options);
    const type = options.type;
    this.setData({
      type,
    });
    if (type === "add") {
      wx.setNavigationBarTitle({ title: "添加订阅" });
    } else {
      wx.setNavigationBarTitle({ title: "编辑订阅" });

      const eventChannel = this.getOpenerEventChannel();
      eventChannel.on("sub_info", (data) => {
        console.log(data.data);
        let info = data.data;
        this.setData({
          id: info.id,
          keys: info.keywords.split(","),
          hangye_type: info.industry_type,
          area: info.areas,
          title: info.industry_name,
        });
      });
    }

    publicApi.getIndustryList().then((res) => {
      this.setData({
        industryList: res.data.map((item) => item.name) || [],
      });
    });

    publicApi.getAreaList().then((res) => {
      let sheng = res.rows.filter((item) => item.level === 1);
      let citys = {};
      sheng.forEach((item) => {
        citys[item.name] = res.rows
          .filter((item2) => item2.pcode === item.code)
          .map((item3) => item3.name);
      });
      console.log("ccc === ", citys);
      let cs = [
        {
          values: Object.keys(citys),
          className: "column1",
        },
        {
          values: citys["北京市"],
          className: "column2",
          defaultIndex: 2,
        },
      ];
      this.setData({
        citys,
        city_columns: cs,
      });
    });
  },
  addKey() {
    if (this.data.mainKey.length > 10) {
      wx.showToast({
        title: "关键词不能超过10个字！",
        icon: "error",
        duration: 2000,
      });
      return;
    }
    let arr = JSON.parse(JSON.stringify(this.data.keys));
    arr.push(this.data.mainKey);
    this.setData({
      keys: arr,
      mainKey: "",
    });
  },
  delTag(event) {
    let index = event.currentTarget.dataset.index;
    let arr = JSON.parse(JSON.stringify(this.data.keys));
    arr.splice(index, 1);
    this.setData({
      keys: arr,
    });
  },
  handleInput(event) {
    this.setData({
      mainKey: event.detail.value,
    });
  },
  showSelector(event) {
    const index = event.currentTarget.dataset.index;
    if (index == 1) {
      this.setData({
        showSelectIndustry: true,
      });
    } else if (index == 2) {
      this.setData({
        showSelectCity: true,
      });
    }
  },
  // 选择行业
  cancelindustry() {
    this.setData({
      showSelectIndustry: false,
    });
  },
  confirmindustry(event) {
    const { picker, value, index } = event.detail;
    console.log("val === ", value);
    this.setData({
      hangye_type: this.data.industryList[index],
      showSelectIndustry: false,
    });
  },
  // 选择城市
  cancelcity() {
    this.setData({
      showSelectCity: false,
    });
  },
  confirmcity(event) {
    const { picker, value, index } = event.detail;
    this.setData({
      area: value.join(","),
      showSelectCity: false,
    });
  },
  changeCity(event) {
    const { picker, value, index } = event.detail;
    picker.setColumnValues(1, this.data.citys[value[0]]);
  },
  handleInput2(event) {
    let val = event.detail.value;
    this.setData({
      title: val,
    });
  },
  save() {
    if (this.data.title.trim().length == 0) {
      wx.showToast({
        title: "请输入标题！",
        icon: "error",
        duration: 2000,
      });
      return;
    }
    if (this.data.keys.length == 0) {
      wx.showToast({
        title: "请输入关键词！",
        icon: "error",
        duration: 2000,
      });
      return;
    }
    if (this.data.hangye_type == "") {
      wx.showToast({
        title: "请选择行业！",
        icon: "error",
        duration: 2000,
      });
      return;
    }
    if (this.data.area == "") {
      wx.showToast({
        title: "请选择城市！",
        icon: "error",
        duration: 2000,
      });
      return;
    }
   if (this.data.type === 'add') {
    mineApi
    .addSubscription({
      industry_name: this.data.title, //行业名称
      subscription_way: 1, //订阅方式，1-标题，2-全文，3-精准订阅
      push_channel: 1, //推送渠道，1-平台消息推送，2-微信公众号，3-邮件
      keywords: this.data.keys.join(","), //关键词
      areas: this.data.area, //地区
      industry_type: this.data.hangye_type, //行业类型
      amount_min: 100000, //金额最小值
      amount_max: 1000000, //金额最大值
    })
    .then((res) => {
      console.log("res === ", res);
      wx.showToast({
        title: "添加成功！",
        icon: "success",
        duration: 2000,
      });
      wx.navigateBack({
        delta: 1,
      });
    })
    .catch((err) => {
      wx.showToast({
        title: "添加失败！",
        icon: "error",
        duration: 2000,
      });
    });
   } else {
    mineApi.updateSubscription({
      industry_name: this.data.title, //行业名称
      subscription_way: 1, //订阅方式，1-标题，2-全文，3-精准订阅
      push_channel: 1, //推送渠道，1-平台消息推送，2-微信公众号，3-邮件
      keywords: this.data.keys.join(","), //关键词
      areas: this.data.area, //地区
      industry_type: this.data.hangye_type, //行业类型
      amount_min: 100000, //金额最小值
      amount_max: 1000000, //金额最大值
    }) .then((res) => {
      console.log("res === ", res);
      wx.showToast({
        title: "修改成功！",
        icon: "success",
        duration: 2000,
      });
      wx.navigateBack({
        delta: 1,
      });
    })
    .catch((err) => {
      wx.showToast({
        title: "修改失败！",
        icon: "error",
        duration: 2000,
      });
    });
   }
  },
});
